import java.util.Arrays;

//8-1课堂笔记 二分查找，Arrays.equal,Arrays.fill
public class Test {
    public static void main(String[] args) {
        int[] arry = {1, 4, 2, 8, 6};
        Arrays.sort(arry);
        System.out.println(Arrays.toString(arry));
        //二分查找(元素下标)
        System.out.println("二分查找  前提是数组元素有序");
        int ret=halffind(arry,8);
        System.out.println(ret);

        //
        System.out.println("用库函数查找元素下标");
        int index=Arrays.binarySearch(arry,8);
        System.out.println(index);
    }

    //二分查找   前提是数组元素有序
    public static int halffind(int[] arr, int key) {
        int left = 0;
        int right = arr.length - 1;

        while (left <= right) {    //left==right的时候是找到最左边，或者最右边的元素
            int mid = (left + right) / 2;
            if (key > arr[mid]) {
                left = mid + 1;
            } else if (key < arr[mid]) {
                right = mid - 1;
            } else {
                return mid;//key found
            }
        }
        return -1;//key not found
    }
}

class Test2 {
    public static void main(String[] args) {
        int[] arr1={1,2,3,5};
        int[] arr2={1,2,3,5};

        //Arrays.equals比较数组是否相同方法
        System.out.println("Arrays.equals比较数组是否相同方法");
        boolean ret=Arrays.equals(arr1,arr2);
        System.out.println(ret);

        //用Arrays.fill初始化为某个值
        System.out.println("用Arrays.fill初始化为某个值");
        int[] arr3=new int[10];
        Arrays.fill(arr3,-1);
        System.out.println(Arrays.toString(arr3));
    }
}








